import re
import requests

url = 'http://127.0.0.1:8000/admin/login/?next=/admin/'
response = requests.get(url)
csrfmiddlewaretoken = re.search(r'<input type="hidden" name="csrfmiddlewaretoken" value="(.*?)">',response.text).group(1)
cookies = response.cookies
response = requests.post(url, data={
    'csrfmiddlewaretoken': csrfmiddlewaretoken,
    'username': 'admin',
    'password': '123456',
},
    cookies=cookies
)
print(response.text)
